
// 2020-8-4  byby高手
#include <iostream>
#include <string>
#include <vector>
#define PLUS "+"
#define MINUS "-"
#define NONE ""

std::vector<std::string> g_vecOperator;

void GetAllString(std::string &str, int i, std::vector<std::string> &_m_vecList)
{

    if (i > 1)
    {
        for (int t = 0; t < g_vecOperator.size(); t++)
        {
            std::string NewStr = str;
            NewStr.insert(i, g_vecOperator[t]);
            GetAllString(NewStr, i - 1, _m_vecList);
        }
    }
    else if (i == 1)
    {
        for (int t = 0; t < g_vecOperator.size(); t++)
        {
            std::string EndStr = str;
            EndStr.insert(1, g_vecOperator[t]);
            _m_vecList.push_back(EndStr);
        }
    }
    return;
}

int CalculateString(std::string strCalculate)
{
    std::string strNum1{}, strNum{}, op1{}, op2{};
    int num1 = 0, num2 = 0;
    int i, StartIndex = 0;
   
    auto CalculateTwo = [&]()mutable {
          if (op1 == "+")
                {
                    num1 = num1 + num2;
                }
                else if (op1 == "-")
                {
                    num1 = num1 - num2;
                }
    };
    for (i = 0; i < strCalculate.size(); i++)
    {
        if (strCalculate[i] == '+' || strCalculate[i] == '-')
        {
            
            strNum = strCalculate.substr(StartIndex , i - StartIndex );
            StartIndex = i + 1;
            op2 = strCalculate[i];
             num2 = atoi(strNum.c_str());
            if (!op1.empty())
            {           
               CalculateTwo();
                op1 = op2;
            }
            else
            {
                num1 = num2;
                op1 = op2;
            }
        }
      
    }
    strNum = strCalculate.substr(StartIndex, i - StartIndex); //计算最后一个
    num2 = atoi(strNum.c_str());
    CalculateTwo();
    return num1;
}

int main()
{
    g_vecOperator.push_back(PLUS);
    g_vecOperator.push_back(MINUS);
    g_vecOperator.push_back(NONE);
    std::string str = "123456789";
    std::vector<std::string> m_vecList{};
    GetAllString(str, str.size() - 1, m_vecList);
    int i = 0;
    int SUM = 99;
    std::cout<<"在数字 \"" << str <<"\" 中添加任意的 + 或 - 使得最终结果为:"<<SUM<<std::endl;
    for (auto itStr : m_vecList)
    {
        if(CalculateString(itStr) == SUM)
        {
            std::cout<<itStr<<" = "<<SUM<<std::endl;
            i ++;
        }
    }
    std::cout<<"一共有"<<i<<"种结果"<<std::endl;
}